package com.junbao.message.ui.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.junbao.commom.context.ClassPathXmlApplicationContext;
import com.junbao.commom.exception.JunBaoException;
import com.junbao.message.application.service.MessageService;
import com.junbao.message.domain.enums.ProducerName;
import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.infrastructure.core.RequestDto;
import com.junbao.message.infrastructure.core.dispatcher.MessageReserveDisruptor;
import com.junbao.message.infrastructure.core.producer.MessageReserveProducer;
import com.junbao.message.infrastructure.result.Result;
import com.junbao.message.ui.dto.BaseMessage;
import com.junbao.message.ui.dto.ReserveMessage;

/**
 * 消息预存储
 * 预存储消息到消息系统
 * @author yilin
 *
 */
@SuppressWarnings("all")
@WebServlet("/reserve")
public class MessageReserveServlet extends BaseServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String requestData = super.getRequestData(request, response);
			//String requestData = "{\"message_no\":\"271"+RandomUtil.getFixLenthString(5)+"01791"+RandomUtil.getFixLenthString(5)+"\",\"notify_way\":\"10\",\"notify_data_type\":\"10\",\"notify_url\":\"http://47.92.107.201:8080/message/test/handle\",\"notify_data\":\"测试数据\",\"retry_max_count\":\"3\",\"confirm_url\":\"http://47.92.107.201:8080/message/test/confirm\"}";
			BaseMessage reserveMessage = new ReserveMessage(requestData);
			//检查消息是否存在
			MessageService messageService = (MessageService) ClassPathXmlApplicationContext.getInstance().getBean("messageService");
			NotifyMessage messageRecord = messageService.queryMessageByMessageNo(reserveMessage.getMessageNo());
			if (messageRecord != null) {
				Result resultData = new Result(Result.MESSAGE_NO_EXIST.getResult_code(), Result.MESSAGE_NO_EXIST.getResult_msg())
				.putData("message_no", messageRecord.getMessageNo())
				.putData("message_squece", messageRecord.getMessageSquece());
				writer(response, resultData);
				return;
			}
			MessageReserveDisruptor.instance()
			.getMessageProducer(ProducerName.MESSAGE_RESERVE_PRODUCER)
			.sendData(new RequestDto<NotifyMessage>(reserveMessage.getNotifyMessage()));
			Result result = new Result(Result.SUCCESS.getResult_code(), Result.SUCCESS.getResult_msg())
			.putData("message_no", reserveMessage.getMessageNo())
			.putData("message_squece", reserveMessage.getMessageSquece());
			writer(response, result);
			return;
		} catch (JunBaoException e) {
			Result resultData = new Result(e.getErrCode(), e.getErrMsg());
			writer(response, resultData);
			return;
		} catch (Exception e) {
			Result resultData = new Result("9999", e.getMessage());
			writer(response, resultData);
			return;
		}
	}
	
}
